package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.model.Admin;
import com.model.Role;
import com.opensymphony.xwork2.ActionSupport;
import com.util.ConstantUtil;

public class DefaultAction extends ActionSupport {
	
	String adminKeyValue;
	String adminLoginName="";
	String currentMenuId = "";
	String isUpdate = "false";
	public Admin currentAdmin;
	String bottom = "版权所有";
	public DefaultAction()
	{
		if(getSession(ConstantUtil.ACCOUNTSESSIONNAME)!=null)
		{
			currentAdmin = (Admin)getSession(ConstantUtil.ACCOUNTSESSIONNAME);
			adminKeyValue = currentAdmin.getKeyValue();
			adminLoginName = currentAdmin.getLoginName();
			if(getParameter("currentMenuId")!=null)
				currentMenuId = getParameter("currentMenuId");
			if(currentMenuId.equals(""))
			{
				if(getSession(ConstantUtil.CURRENTMENUID)!=null)
				{
					currentMenuId = getSession(ConstantUtil.CURRENTMENUID).toString();
				}
			}
			if(currentMenuId!=null && !"".equals(currentMenuId))
			{
				setSession(ConstantUtil.CURRENTMENUID, currentMenuId);
				if(getSession(ConstantUtil.ROLESESSIONNAME)!=null)
				{
					Role modelrole = (Role)getSession(ConstantUtil.ROLESESSIONNAME);
					if(modelrole.getMenuValue().indexOf("_"+currentMenuId+"update")>-1)
					{
						isUpdate = "true";
					}
						
				}
			}
		}
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = -561746584897019916L;
	

	protected HttpServletRequest getRequest()
	{
		return ServletActionContext.getRequest();
	}
  
	protected HttpServletResponse getResponse()
	{ 
		return ServletActionContext.getResponse();
	}
	
	protected InputStream getInputStream()
	{
		try 
		{
			return getRequest().getInputStream();
		} catch (IOException e)  
		{
			e.printStackTrace();
		}
		
		return null;
	}
	
	protected Map<String, String> getHeaders()
	{
		Map<String, String> headers = new HashMap<String, String>();
		Enumeration<String> e = getRequest().getHeaderNames();
		while (e.hasMoreElements())
		{
			String name = e.nextElement();
			headers.put(name, getRequest().getHeader(name));
		} 
		return headers;
	}
	
	public boolean checkQueryDimension(String dimension, String key)
	{
		if (dimension == null)
			return false;
		String d[] = dimension.split(";");
		for (int n = 0; n < d.length; n++)
			if (d[n].equals(key))
				return true;
		return false;
	}
	public int getQueryDimensionCount(String dimension)
	{
		if (dimension == null)
			return 0;
		int dc = 0;
		String d[] = dimension.split(";");
		for (int n = 0; n < d.length; n++)
			if (!d[n].equals("false"))
				dc++;
		return dc;
	}
	
	protected String getHeader(String key)
	{
		return getRequest().getHeader(key);
	}
	
	protected void setHeader(String key, String value)
	{
		getResponse().setHeader(key, value);
	}
	
	protected String getContentType()
	{
		return getRequest().getContentType();
	}
	
	protected int getContentLength()
	{
		return getRequest().getContentLength();
	}
	
	protected String getContextPath()
	{
		return getRequest().getContextPath();
	}
	
	protected void setContentType(String contentType)
	{
		getResponse().setContentType(contentType);
	}
	
	protected void setContentLength(int contentLength)
	{
		getResponse().setContentLength(contentLength);
	}
	 
	protected void setSession(String key, Object value)
	{
		getRequest().getSession().setAttribute(key, value);
	}
	
	protected Object getSession(String key)
	{
		return getRequest().getSession().getAttribute(key);
	} 
	
	protected String getRemoteAddr()
	{
		return getRequest().getRemoteAddr();
	}

	protected String getRemoteHost()
	{
		return getRequest().getRemoteHost();
	}

	protected int getRemotePort()
	{
		return getRequest().getRemotePort();
	}

	protected Map<String, String[]> getParameters()
	{
		return getRequest().getParameterMap();
	}
	
	protected String getParameter(String paramName)
	{
		return getRequest().getParameter(paramName);
	}

	protected Object getAttribute(String paramName)
	{
		return getRequest().getAttribute(paramName);
	}
	
	protected String getRealPath(String path)
	{
		return ServletActionContext.getServletContext().getRealPath(path);
	}
	
	protected Admin getAdmin() {
		return (Admin)getSession(ConstantUtil.ACCOUNTSESSIONNAME);
	}

	protected int saveUploadFile(File uploadFile, String savePath) 
	{
		int fileLen = 0;
		FileOutputStream fos = null;
		try
		{
			fos = new FileOutputStream(savePath);
	        FileInputStream fis = new FileInputStream(uploadFile);
	        byte[] buffer = new byte[1024];
	        int len = 0;
	        while((len = fis.read(buffer))>0)
	        {
	        	fileLen += len;
	            fos.write(buffer, 0, len);
	        }
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		} finally{
			if(fos!=null)
			{
				try{
				fos.flush();
				fos.close();
				}catch(Exception ex)
				{}
			}
		}
        return fileLen;
	}

	public String getAdminKeyValue() {
		return adminKeyValue;
	}

	public void setAdminKeyValue(String adminKeyValue) {
		this.adminKeyValue = adminKeyValue;
	}

	public String getAdminLoginName() {
		return adminLoginName;
	}

	public void setAdminLoginName(String adminLoginName) {
		this.adminLoginName = adminLoginName;
	}

	public String getCurrentMenuId() {
		return currentMenuId;
	}

	public void setCurrentMenuId(String currentMenuId) {
		this.currentMenuId = currentMenuId;
	}

	public String getIsUpdate() {
		return isUpdate;
	}

	public void setIsUpdate(String isUpdate) {
		this.isUpdate = isUpdate;
	}

	public Admin getCurrentAdmin() {
		return currentAdmin;
	}

	public void setCurrentAdmin(Admin currentAdmin) {
		this.currentAdmin = currentAdmin;
	}
	public String urlencode(String s)
	{
		//System.out.println(s);
		return URLEncoder.encode(s);
	}
	
	/**
	 * ƴ���ַ�
	 * @param str
	 * @return
	 */
	public String getStr(String[] str)
	{
		String s = "";
		if(str!=null && str.length>0)
		{
			for (String string : str) {
				s+=string+",";
			}
		}
		return s;
	}

	public String getBottom() {
		return bottom;
	}

	public void setBottom(String bottom) {
		this.bottom = bottom;
	}
}
